<?php
include './connect.php';
if(isset($_POST["txtSupplierCode"])){
    $strSQL = "SELECT * FROM `supplier` WHERE SupplierCode = '".$_POST["txtSupplierCode"]."'";
    $objQuery = mysqli_query($MechanicDB,$strSQL) or die(mysqli_error($MechanicDB)) ;
    if($objQuery->num_rows > 0){
        echo 'duplicate';
        return false;
    }
    
    $strSQL = "INSERT INTO `supplier`(`SupplierCode`, `SupplierName`, `Tel`, `Address`, `remark`) ";
    $strSQL .= "VALUES ('".$_POST["txtSupplierCode"]."','".$_POST["txtSupplierName"]."','".$_POST["txtTel"]."','".$_POST["txtAddress"]."','".$_POST["txtRemark"]."')";
    //$strSQL .= " VALUES ("..","..","..")";
    mysqli_query($MechanicDB,$strSQL) or die(mysqli_error($MechanicDB)) ;
    
    echo 'success';
    return false;
}
?>
<html>
    <head>
        <?php
            include './sethead.php';
        ?>
        <title>Add New Supplier</title>
        <script>
            $(document).ready(function(){
               $("#btClose").click(function(){
                  //alert("close");
                  parent.$.fancybox.close();
               });
               $("#btSubmit").click(function(){
                   if($("#btSupplierCode").val()==""){
                       $.Zebra_Dialog('Please input Supplier Code',{
                        'type':'warning',
                        'title':'Warning',
                        onClose:function(){
                            $("#txtSupplierCode").focus();
                        }
                    });
                    return false;
                   }
                    if($("#btSupplier").val()==""){
                       $.Zebra_Dialog('Please input Supplier',{
                        'type':'warning',
                        'title':'Warning',
                        onClose:function(){
                            $("#txtSupplier").focus();
                        }
                    });
                    return false;
                   }
                   $.post(
                       "frmAddSupplier.php",
                       {
                            txtSupplierCode : $("#txtSupplierCode").val() ,
                            txtSupplierName : $("#txtSupplierName").val(),
                            txtTel: $("#txtTel").val(),
                            txtAddress : $("#txtAddress").val(),
                            txtRemark : $("#txtRemark").val()
                       },
                       function(data){
                           if(data == "success"){
                                $.Zebra_Dialog('Insert Success',{'type':'confirmation','title':'Succress'});
                            }else if(data == "duplicate"){
                                $.Zebra_Dialog('SupplierCode is Duplicate',{'type':'warning','title':'Duplicate'});
                            }else{
                                $.Zebra_Dialog('Insert Error',{'type':'error','title':'Error'});
                            }
                          //alert(data);
                       }
                   );
               });
            });
        </script>
    </head>
    <body class="center">
        <div class="headpage">
            Add New Supplier
        </div>
        <div class="bodypage">
            <table border='0' >
                <tr>
                        <td class='right'>Supplier Code : </td>
                        <td><input type='text' class='text-m' id="txtSupplierCode"></td>
                </tr>
                <tr>
                        <td class='right'>Supplier Name : </td>
                        <td><input type='text' class='text-m' id="txtSupplierName"></td>
                </tr>
                <tr>
                        <td class='right'>Tel : </td>
                        <td><input type='text' class='text-m' id="txtTel"></td>
                </tr>
                <tr>
                        <td class='right'>Address : </td>
                        <td><input type='text' class='text-m' id="txtAddress"></td>
                </tr>
                <tr>
                        <td class='right'>Remark : </td>
                        <td><textarea cols='30' id="txtRemark"></textarea></td>
                </tr>
                <tr>
                    <td colspan="2" class="center">
                        <button id="btSubmit">Submit</button>
                        <button id="btClose">Close</button>
                    </td>
                </tr>
            </txble>
        </div>
    </body>    
</html>